package com.naiterui.ehp.bs.patient.dao.mongo;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import com.naiterui.common.repository.page.Page;
import com.naiterui.ehp.bs.patient.prodocl.MCaseFile;

/**
 * 患者病例档案持久化。
 * <P>File name : PatientCaseFileDao.java </P>
 * <P>Author : NTR.Amber </P>
 * <P>Date : 2015年9月13日 </P>
 */
@Repository
public class ApPatientCaseFileDao extends MongoBaseDao {
    /**
     * <患者病例档案记录的collectionName>
     */
    private static final String COLLECTION_NAME = "m_patient_case";

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    protected MongoTemplate getMongoTemplate() {
        return this.mongoTemplate;
    }

    /**
     * 分页获取患者病例档案
     * PatientCaseFileDao.getCaseByPage()<BR>
     * <P>Author : Amber </P>
     * <P>Date : 2015年9月13日 </P>
     *
     * @param patientId
     * @param page
     * @param num
     *
     * @return
     */
    public Page<MCaseFile> getCaseByPage(Long patientId, int page, int num) {
        Query query = new Query();
        Criteria criteria = new Criteria("patientId").is(patientId);
        query.addCriteria(criteria);

        // 查询
        query = Query.query(criteria).with(Sort.by(new Sort.Order(Direction.DESC, "vistingTime"), new Sort.Order(Direction.DESC, "createdAt")));
        List<MCaseFile> caseFiles = super.find(query, MCaseFile.class, COLLECTION_NAME);
        int totalCount = caseFiles.size();
        // 返回值
        Page<MCaseFile> pageCaseFilePage = new Page<>();
        pageCaseFilePage.setPageNo(page);
        pageCaseFilePage.setPageSize(num);

        pageCaseFilePage.setTotalCount(totalCount);
        int start = Page.getStartOfPage(pageCaseFilePage.getPageNo(), pageCaseFilePage.getPageSize());
        int end = start + pageCaseFilePage.getPageSize();
        List<MCaseFile> pageValues = caseFiles.subList(start, end > caseFiles.size() ? caseFiles.size() : end);

        pageCaseFilePage.setResult(pageValues);

        return pageCaseFilePage;
    }

    /**
     * 获取患者某个病例档案详情。
     * PatientCaseFileDao.getCaseByPatient()<BR>
     * <P>Author : Amber </P>
     * <P>Date : 2015年9月13日 </P>
     *
     * @param patientId
     * @param caseId
     *
     * @return
     */
    public MCaseFile getCaseByPatient(Long patientId, String caseId) {
        Query query = new Query();
        query.addCriteria(new Criteria("_id").is(caseId).and("patientId").is(patientId));
        List<MCaseFile> caseFiles = super.find(query, MCaseFile.class, COLLECTION_NAME);
        return caseFiles.size() > 0 ? caseFiles.get(0) : null;
    }

    /**
     * 获取患者第一个病例
     * PatientCaseFileDao.getFirstCase()
     *
     * @param patientId
     *
     * @return
     *
     * @Author Amber
     * @Date 2016年1月14日
     * @since 1.0.0
     */
    public MCaseFile getFirstCase(Long patientId) {
        Query query = new Query();
        Criteria criteria = new Criteria("patientId").is(patientId);
        query.addCriteria(criteria);

        // 查询
        query = Query.query(criteria).with(Sort.by(new Sort.Order(Direction.ASC, "createdAt")));
        List<MCaseFile> caseFiles = super.find(query, MCaseFile.class, COLLECTION_NAME);
        return caseFiles.size() > 0 ? caseFiles.get(0) : null;
    }

    /**
     * 保存并返回最新实体
     * PatientCaseFileDao.saveCaseFile()<BR>
     * <P>Author : Amber </P>
     * <P>Date : 2015年9月14日 </P>
     *
     * @param caseFile
     */
    public void saveOrUpdate(MCaseFile caseFile) {
        if (caseFile.getId() == null) {
            caseFile.setCreatedAt(new Date());
            super.save(caseFile, COLLECTION_NAME);
        } else {
            this.updateCaseFile(caseFile);
        }
    }

    /**
     * 更新病例档案
     * PatientCaseFileDao.updateCaseFile()<BR>
     * <P>Author : Amber </P>
     * <P>Date : 2015年9月14日 </P>
     *
     * @param caseFile
     */
    public void updateCaseFile(MCaseFile caseFile) {
        Query query = new Query();
        query.addCriteria(new Criteria("_id").is(caseFile.getId()));
        Update update = new Update();
        update.set("doctor", caseFile.getDoctor());
        update.set("description", caseFile.getDescription());
        update.set("departmentId", caseFile.getDepartmentId());
        update.set("department", caseFile.getDepartment());
        update.set("hospitalId", caseFile.getHospitalId());
        update.set("hospital", caseFile.getHospital());
        update.set("vistingTime", caseFile.getVistingTime());
        update.set("adviceList", caseFile.getAdviceList());
        update.set("prescriptionList", caseFile.getPrescriptionList());
        update.set("checkList", caseFile.getCheckList());
        update.set("changedAt", new Date());
        update.set("changedBy", caseFile.getChangedBy());
        super.update(MCaseFile.class, COLLECTION_NAME, query, update);
    }

    /**
     * 患者删除病例档案
     * PatientCaseFileDao.deleteCaseFile()<BR>
     * <P>Author : Amber </P>
     * <P>Date : 2015年9月14日 </P>
     *
     * @param caseFile
     */
    public void deleteCaseFile(MCaseFile caseFile) {
        super.remove(caseFile, COLLECTION_NAME);
    }

    /**
     * 查看病历档案
     * PatientCaseFileDao.getById()<BR>
     * <P>Author : Amber </P>
     * <P>Date : 2015年9月14日 </P>
     *
     * @param caseId
     *
     * @return
     */
    public MCaseFile getById(String caseId) {
        Query query = new Query();
        query.addCriteria(new Criteria("_id").is(caseId));
        List<MCaseFile> caseFiles = super.find(query, MCaseFile.class, COLLECTION_NAME);
        return caseFiles.size() > 0 ? caseFiles.get(0) : null;
    }
}
